/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import type.MessageType;
import util.InternalMessageEntityListener;

/**
 *
 * @author wei
 */
@Entity(name="InternalMessage")
@EntityListeners(InternalMessageEntityListener.class)
public class InternalMessageEntity extends DataObjectEntity implements Serializable, Comparable<InternalMessageEntity> {

    private String title;
    @Lob
    private String body = "";
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date sentAt;
    private MessageType messageType;
    private boolean forwaredToEmail;
    
    //the many side must be the owner of the relationship
    @ManyToOne
    private SystemUserEntity sender; 
    
    @ManyToMany
    @JoinTable(name="MESSAGE_RECIPIENT")
    private Collection<SystemUserEntity> recipients = new HashSet<SystemUserEntity>();
    
    @ManyToMany(fetch= FetchType.EAGER)
    @JoinTable(name="MESSAGE_DEPARTMENT_RECIPIENT")
    private Collection<DepartmentEntity> departmentRecipients = new HashSet<DepartmentEntity>();
    
    @Transient
    private boolean read;

    public InternalMessageEntity() {
        super();
    }
    
    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Collection<SystemUserEntity> getRecipients() {
        return recipients;
    }

    public void setRecipients(Collection<SystemUserEntity> recipients) {
        this.recipients = recipients;
    }

    public SystemUserEntity getSender() {
        return sender;
    }

    public void setSender(SystemUserEntity sender) {
        this.sender = sender;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public MessageType getMessageType() {
        return messageType;
    }

    public void setMessageType(MessageType messageType) {
        this.messageType = messageType;
    }

    public Date getSentAt() {
        return sentAt;
    }

    public void setSentAt(Date sentAt) {
        this.sentAt = sentAt;
    }

    public boolean isForwaredToEmail() {
        return forwaredToEmail;
    }

    public void setForwaredToEmail(boolean forwaredToEmail) {
        this.forwaredToEmail = forwaredToEmail;
    }

    public Collection<DepartmentEntity> getDepartmentRecipients() {
        return departmentRecipients;
    }

    public void setDepartmentRecipients(Collection<DepartmentEntity> departmentRecipients) {
        this.departmentRecipients = departmentRecipients;
    }

    public boolean isRead() {
        return read;
    }

    public void setRead(boolean read) {
        this.read = read;
    }
    
    public String getRecipientToString(){
        String res = "";
        for(SystemUserEntity u : this.getRecipients()){
            res += u.getName() + "; ";
        }
        return res;
    }
    
    public String getDepartmentRecipientToString(){
        String res = "";
        for(DepartmentEntity u : this.getDepartmentRecipients()){
            res += u.getName() + "; ";
        }
        return res;
    }

    @Override
    public int compareTo(InternalMessageEntity t) {
        int res = 0;
        if(this.getSentAt()==null && t.getSentAt()!=null)
            res = 1;
        else if(this.getSentAt()!=null && t.getSentAt()==null)
            res = -1;
        else if (this.getSentAt()!=null && t.getSentAt()!=null)
            res = this.getSentAt().compareTo(t.getSentAt());
        
        return res;
    }

    @Override
    public String childToString() {
        return "InternalMessageEntity{" + "title=" + title + ", body=" + body + ", sentAt=" + sentAt + ", messageType=" + messageType + ", forwaredToEmail=" + forwaredToEmail + ", sender=" + sender + ", recipients=" + recipients + ", departmentRecipients=" + departmentRecipients + ", read=" + read + '}';
    } 
    
}
